﻿/******************************************************************
** 文 件 名: Bu_AuditBatch_List.aspx.cs
** Copyright (c) 2010-2012 Honestysoft (GuangZhou) Co.,LTD
** 创 建 人: 此代码由生成器自动生成
** 创建日期: 2012-3-21 15:53:10
** 描    述: 资金申请批次列表页面
** 版    本: 2.0.0.0
** 修改描述:
** 修 改 人:
** 修改日期:
******************************************************************/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Text;
using Honestysoft.BaseLibrary;
using Honestysoft.BaseObject;
using Honestysoft.BaseLibrary.ExceptionHandling;
using Honestysoft.Baseframework.WebUI;
using Honestysoft.Financing.Model;
using System.Data.Common;

namespace Honestysoft.Financing.WebUI.Module.Audit
{
	public partial class Bu_AuditBatch_List : BasePage
	{	
		protected override void Page_Load(object sender, EventArgs e)
        {
			base.Page_Load(sender, e);
            if (!IsPostBack)
            {		
				AspNetPager1.PageSize = Globals.PageSize;
				BindControl();
                Bind();
            }
        }
		/// <summary>
        /// 绑定页面控件。
        /// </summary>
		private void BindControl()
		{
			ControlHelper.BindEnum(ddlBatchStatus, "BatchStatus");//请修改成typeof(EnumItem.XXX)
			ControlHelper.InsertAllItemAndSelect(ref ddlBatchStatus);
		}
		/// <summary>
        /// 设置弹出窗体大小
		/// 基类中已自动做控制。默认宽度800，高度600
        /// </summary>
		protected override void SetDialogSize()
		{
            //base.SetDialogSize();
			//重载宽度，高度
            ModalDialogSize.EditHeight = 400;
            ModalDialogSize.EditWidth = 500;
            ModalDialogSize.ViewHeight = 400;
            ModalDialogSize.ViewWidth = 500;
		}
		/// <summary>
        /// 根据权限控制页面按钮.
        /// 基类中已自动做控制。
        /// </summary>
        protected override void ButtonSetting()
        {
            base.ButtonSetting();
            //使用PageSecurity检查权限
            if (PageSecurity.Add)
            {
                btnAdd.Attributes.Add("onclick", "javascript:openDialog2();return false;");
            }
        }
		/// <summary>
        /// 绑定列表
        /// </summary>
		private void Bind()
        {
            //如果当前存在没有输出的批次，则不能再新增
            QueryParameters query = new QueryParameters();
            query.Add("BatchStatus", ((int)EnumItem.BatchStatus.Output).ToString(), QueryParameters.OperatorType.NotEqual);
            List<Bu_AuditBatch> lsAct = new BaseBusinessLogic<Bu_AuditBatch>(Globals.CurrentConnStringName).GetEntitys(query);
            if (lsAct != null && lsAct.Count > 0)
            {
                hidIsActive.Value = "1";
            }
            else
            {
                hidIsActive.Value = "0";
            }

            Honestysoft.Financing.BusinessLogic.Audit bll = new BusinessLogic.Audit(Globals.CurrentConnStringName);
            int? batch = null;
            if (numBatchNo.Text.Trim().Length > 0)
            {
                batch = Convert.ToInt32(numBatchNo.Text.Trim());
            }
            string status = "";
            if (ddlBatchStatus.SelectedValue != "0")
            {
                status = ddlBatchStatus.SelectedValue;
            }
            int pageCount = 0, recordCount = 0;
            DataTable dtList = bll.GetBatchs(batch, status, AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex, out pageCount, out recordCount);
            AspNetPager1.RecordCount = recordCount;
            rptList.DataSource = dtList;
            rptList.DataBind();
        }

        protected string GetCheckbox(string dataId, string batchStatus)
        {
            if (batchStatus != ((int)EnumItem.BatchStatus.Active).ToString())
            {
                return "";
            }
            return "<input type=\"checkbox\" name=\"chkID\" id=\"chk" + dataId + "\"  value=\"" + dataId + "\" >";
        }

		/// <summary>
        /// 得到表格行操作按钮脚本
        /// </summary>
        /// <param name="dataId">记录ID</param>
        /// <param name="batchStatus">批次状态</param>
        /// <returns>操作按钮脚本</returns>
        protected string GetButtonScript(string dataId,string batchStatus)
        {
			string script = "";

            if (PageSecurity.View || PageSecurity.Edit)
            {
                script += ScriptHelper.LinkButton.GetGridImageButton("window.location.href='BatchInfo.aspx?ID=" + dataId + "&FunctionID=" + FunctionID + "'", ScriptHelper.Image.GetUrl(ScriptHelper.Image.ImageType.Edit), "修改资金申请批次", "");
            }
            if (batchStatus == ((int)EnumItem.BatchStatus.Active).ToString())
            {
                if (PageSecurity.Delete)
                {
                    if (script.Length > 0) { script += "&nbsp;"; }
                    script += ScriptHelper.LinkButton.GetGridDeleteButton(dataId, btnDelete.ClientID);
                }
            }

            //if (PageSecurity.Edit)
            //{
            //    script += ScriptHelper.LinkButton.GetGridImageButton("openEditDialog('" + dataId + "');", ScriptHelper.Image.GetUrl(ScriptHelper.Image.ImageType.Edit), "修改资金申请批次", "");
            //    //script += ScriptHelper.LinkButton.GetGridEditButton(Globals.Home + "Module/Audit/Bu_AuditBatch_Edit.aspx", "修改资金申请批次", ModalDialogSize.EditWidth, ModalDialogSize.EditHeight, dataId, FunctionID);
            //}
            
            //if (PageSecurity.View)
            //{
            //    if (script.Length > 0) { script += "&nbsp;"; }
            //    script += ScriptHelper.LinkButton.GetGridViewButton(Globals.Home + "Module/Audit/Bu_AuditBatch_View.aspx", "查看资金申请批次", ModalDialogSize.ViewWidth, ModalDialogSize.ViewHeight, dataId, FunctionID);
            //}
            return script;
        }
		
        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            Bind();
        }
		
		protected void btnQuery_Click(object sender, EventArgs e)
        {
            Bind();
        }
		/// <summary>
        /// 得到选中的记录ID集合
        /// </summary>
        /// <returns></returns>
        private string[] GetSelectedRowKeys()
        {
            string strId = Request["chkID"]+"";
            if (string.IsNullOrEmpty(strId)) { return null; }
            return strId.Split(',');
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            DbTransaction transaction = Honestysoft.BaseLibrary.Data.DatabaseProvider.GetDbTransaction(Globals.CurrentConnStringName);
            DbConnection connection = transaction.Connection;
            try
            {
                string[] arrId = GetSelectedRowKeys();
                BaseBusinessLogic<Bu_AuditBatch> baseLogic = new BaseBusinessLogic<Bu_AuditBatch>(Globals.CurrentConnStringName);
                BaseBusinessLogic<Bu_BatchRec> bllBatchRec = new BaseBusinessLogic<Bu_BatchRec>(Globals.CurrentConnStringName);
                Honestysoft.Workflow.BusinessFacade.WFApply.FlowManage IFlowManage = new Workflow.BusinessFacade.WFApply.FlowManage(Globals.CurrentConnStringName);
                QueryParameters query = new QueryParameters();
                query.Add("BatchID", arrId, QueryParameters.OperatorType.IN);
                List<Bu_BatchRec> lsBatchRec = bllBatchRec.GetEntitys(query, transaction);
                List<string> lsTableID = Honestysoft.BaseObject.DataMapping.GetEntitysStringPropertyList<Bu_BatchRec>(lsBatchRec, "TableID");
                IFlowManage.DeleteWorkflow(((int)EnumItem.WFBill.AuditTable).ToString(), lsTableID, transaction);    
                new BaseBusinessLogic<Bu_AuditTable>(Globals.CurrentConnStringName).DeleteByWhere(query, transaction);
                new BaseBusinessLogic<Bu_BatchRec>(Globals.CurrentConnStringName).DeleteByWhere(query, transaction);
                new BaseBusinessLogic<Bu_BatchContract>(Globals.CurrentConnStringName).DeleteByWhere(query, transaction);
                List<string> lsID = new List<string>();
                lsID.AddRange(arrId);
                IFlowManage.DeleteWorkflow(((int)EnumItem.WFBill.Batch).ToString(), lsID, transaction);
                baseLogic.DeleteByKey(arrId, transaction);
                transaction.Commit();
                string script = "var arrNodeId=new Array(" + arrId.Length + ");";
                for (int i = 0; i < arrId.Length; i++)
                {
                    script += "arrNodeId[" + i + "]='" + arrId[i] + "';";
                }
                script += "window.parent.removeChildNode(arrNodeId);";
                ScriptHelper.Window.Alert("删除成功！", script);
                Bind();
            }
            catch (Honestysoft.BaseLibrary.ExceptionHandling.BaseException bex)
            {
                transaction.Rollback();
                ExceptionHelper.ExceptionDistribute(bex, false);
                ScriptHelper.Window.Alert(bex.Message);
            }
            catch (Exception ex)
            {
                transaction.Rollback();
                ExceptionHelper.ExceptionDistribute(ex, false);
                ScriptHelper.Window.Alert("删除失败！系统发生了未知错误！");
            }
            finally
            {
                connection.Close();
            }
        }
	}
}
